package com.byzx.wine.action;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.byzx.wine.po.Winebrand;
import com.byzx.wine.service.IWineBandService;
import com.byzx.wine.util.Page;

@Controller
@RequestMapping(value = "/winebrand")
public class WineBrandController {
	@Autowired 
	IWineBandService wineBandService;

	@RequestMapping("winebrand")
	public String login(HttpServletResponse response, HttpServletRequest request) {
		return "winebrand/winebrand";
	}

	@RequestMapping("findwine")
	@ResponseBody
	public Map<String, Object> findPage(Model model, Integer iDisplayLength, Integer sEcho, Integer iDisplayStart) {
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(iDisplayStart, iDisplayLength, map);
		Page<Map<String, Object>> page1 = wineBandService.findPage(page);

		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}
//添加数据
	@RequestMapping("wineadd")
	@ResponseBody
	public Map<String, Object> insert(Winebrand Winebrand) {
		int num = wineBandService.insert(Winebrand);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("num", num);  //返回一个数值判断是否插入成功
		return map;
	}

	@RequestMapping("/select")
	@ResponseBody
	public Winebrand selectByPrimaryKey(Integer id) {
		
				return wineBandService.selectByPrimaryKey(id);
	}
//更新数据
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> updateByPrimaryKeySelective(Winebrand winebrand) {
		int num = wineBandService.updateByPrimaryKeySelective(winebrand);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("num", num);
		return map;
	}
//单个删除
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> deleteByPrimaryKey(Integer id) {
		int num = wineBandService.deleteByPrimaryKey(id);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("num", num);
		return map;
	}
	//批量删除
	@RequestMapping("/deleteall")
	@ResponseBody
	public Map<String, Object> deleteall(String id) {
		     String shuzu[]=id.split(",");  //去掉，号
		     int num=0;
		    for (int i = 0; i < shuzu.length; i++) {
				int ids=Integer.parseInt(shuzu[i]);
				 num= wineBandService.deleteByPrimaryKey(ids);
			}
		   
		    Map<String,Object> map=new HashMap<String, Object>();
		    map.put("num",num);
		return map;
	}

}
